import {
  getTree
} from '@/api/course/chapter'
export default {
  props: {
    treeOptions: {
      type: [Object, Array],
      default: function() { // 对象或数组默认值必须从一个工厂函数获取
        return {
          show_checkbox: false,
          row_btn: false,
          expand_on_click_node: false,
          check_on_click_node: false
        }
      }
    }
  },
  data() {
    return {
      plusOnce: true,
      rightStyle: {
        first: true,
        show: false,
        top: 100,
        left: 80
      },
      treeDatas: [],
      treeAddId: 1000,
      treeCheckId: null,
      treeChecked: {
        id: null,
        event: null,
        data: null,
        node: null,
        self: null
      },
      treeProps: {
        label: (data, node) => {
          return data.label
        }
      },
      currLevel: '',
      currSignal: {},
      currChannel: {}
    }
  },
  created() {
    if (this.$route.query.courseId) {
      this.getDatas()
    } else {
      console.log('dsa')
    }
  },
  mounted() {
    if (this.treeDatas.length > 0) {
      this.plusOnce = false
    }
  },
  methods: {
    getDatas() {
      getTree({
        courseId: this.$route.query.courseId
      }).then((response) => {
        this.treeDatas = response.data
      })
    },
    node_click(data, node, self) { // 点击菜单标题
      this.treeChecked.node = node
      this.treeChecked.self = self
      this.checkedUnique(data)
      this.$emit('toChapter', data)
      if (this.$route.query.actFrom) {
        this.courseShow(data)
      } else {
        this.$router.push({
          path: 'detail',
          query: {
            courseId: this.$route.query.courseId,
            courseName: this.$route.query.courseName || '',
            courseCatalogId: data.id
          }
        })
      }
    },
    tree_check(data, checkedObj) { // 点击复选框
      this.checkedUnique(data)
    },
    checkedUnique(data) {
      // this.$refs.tree.setCheckedKeys([])
      this.treeChecked.data = data
      if (this.treeChecked.id == data.id) {
        this.$refs.tree.setCheckedKeys([data.id])
      } else {
        this.treeChecked.id = data.id
        this.$refs.tree.setCheckedKeys([data.id])
      }
    },
    courseShow(row) {
      this.$router.push({
        path: '/users/coursechapter',
        query: {
          courseId: row.id,
          courseName: row.label
        }
      })
    }
  }
}
